home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH09 / PGM9_4.ASM < prev   
Encoding:
Assembly Source File  |  1996-02-08  |  3.9 KB  |  204 lines

  1. ; Pgm9_4.ASM
  2. ;
  3. ;    This program demonstrates how to pack and unpack
  4. ;    data types.  It reads in a month, day, and year value.
  5. ;     It then packs these values into the format the textbook
  6. ;    presents in chapter two.  Finally, it unpacks this data
  7. ;    and calls the stdlib DTOA routine to print it as text.
  8.  
  9.         .xlist
  10.         include     stdlib.a
  11.         includelib    stdlib.lib
  12.         .list
  13.  
  14.  
  15. dseg        segment    para public 'data'
  16.  
  17. Month        byte    ?    ;Holds month value (1-12)
  18. Day        byte    ?    ;Holds day value (1-31)
  19. Year        byte    ?    ;Holds year value (80-99)
  20.  
  21. Date        word    ?    ;Packed data goes in here.
  22.  
  23. dseg        ends
  24.  
  25.  
  26.  
  27. cseg        segment    para public 'code'
  28.         assume    cs:cseg, ds:dseg
  29.  
  30.  
  31. ; GETI-    Reads an integer variable from the user and returns its
  32. ;    its value in the AX register.
  33.  
  34. geti        textequ    <call _geti>
  35. _geti        proc
  36.         push    es
  37.         push    di
  38.  
  39.         getsm
  40.         atoi
  41.         free
  42.  
  43.         pop    di
  44.         pop    es
  45.         ret
  46. _geti        endp
  47.  
  48.  
  49. Main        proc
  50.         mov    ax, dseg
  51.         mov    ds, ax
  52.         mov    es, ax
  53.         meminit
  54.  
  55.  
  56.         print
  57.         byte    "Date Conversion Program",cr,lf
  58.         byte    "-----------------------",cr,lf
  59.         byte    lf,0
  60.  
  61.  
  62. ; Get the month value from the user.
  63. ; Do a simple check to make sure this value is in the range
  64. ; 1-12.  Make the user reenter the month if it is not.
  65.  
  66. GetMonth:    print
  67.         byte    "Enter the month (1-12): ",0
  68.  
  69.         geti
  70.         mov    Month, al
  71.         cmp    ax, 0
  72.         je    BadMonth
  73.         cmp    ax, 12
  74.         jbe    GoodMonth
  75. BadMonth:    print
  76.         byte    "Illegal month value, please re-enter",cr,lf,0
  77.         jmp    GetMonth
  78.  
  79. GoodMonth:
  80.  
  81.  
  82. ; Okay, read the day from the user.  Again, do a simple
  83. ; check to see if the date is valid.  Note that this code
  84. ; only checks to see if the day value is in the range 1-31.
  85. ; It does not check those months that have 28, 29, or 30
  86. ; day months.
  87.  
  88. GetDay:        print
  89.         byte    "Enter the day (1-31): ",0
  90.         geti
  91.         mov    Day, al
  92.         cmp    ax, 0
  93.         je    BadDay
  94.         cmp    ax, 31
  95.         jbe    GoodDay
  96. BadDay:        print
  97.         byte    "Illegal day value, please re-enter",cr,lf,0
  98.         jmp    GetDay
  99.  
  100. GoodDay:
  101.  
  102.  
  103. ; Okay, get the year from the user.
  104. ; This check is slightly more sophisticated.  If the user
  105. ; enters a year in the range 1980-1999, it will automatically
  106. ; convert it to 80-99.  All other dates outside the range
  107. ; 80-99 are illegal.
  108.  
  109. GetYear:    print
  110.         byte    "Enter the year (80-99): ",0
  111.         geti
  112.         cmp    ax, 1980
  113.         jb    TestYear
  114.         cmp    ax, 1999
  115.         ja    BadYear
  116.  
  117.         sub    dx, dx        ;Zero extend year to 32 bits.
  118.         mov    bx, 100
  119.         div    bx        ;Compute year mod 100.
  120.         mov    ax, dx
  121.         jmp    GoodYear
  122.  
  123. TestYear:    cmp    ax, 80
  124.         jb    BadYear
  125.         cmp    ax, 99
  126.         jbe    GoodYear
  127.  
  128. BadYear:    print
  129.         byte    "Illegal year value.  Please re-enter",cr,lf,0
  130.         jmp    GetYear
  131.  
  132. GoodYear:    mov    Year, al
  133.  
  134.  
  135. ; Okay, take these input values and pack them into the following
  136. ; 16-bit format:
  137. ;
  138. ;      bit 15     8 7      0
  139. ;          |      | |      |
  140. ;       MMMMDDDD DYYYYYYY
  141.  
  142.  
  143.         mov    ah, 0
  144.         mov    bh, ah
  145.         mov    al, Month    ;Put Month into bit positions
  146.         mov    cl, 4        ; 12..15
  147.         ror    ax, cl
  148.  
  149.         mov    bl, Day        ;Put Day into bit positions
  150.         mov    cl, 7        ; 7..11.
  151.         shl    bx, cl
  152.  
  153.         or    ax, bx        ;Create MMMMDDDD D0000000
  154.         or    al, Year    ;Create MMMMDDDD DYYYYYYY
  155.         mov    Date, ax    ;Save away packed date.
  156.  
  157. ; Print out the packed date (in hex):
  158.  
  159.         print
  160.         byte    "Packed date = ",0
  161.         putw
  162.         putcr
  163.  
  164. ; Okay, the following code demonstrates how to unpack this date
  165. ; and put it in a form the standard library's LDTOAM routine can
  166. ; use.
  167.  
  168.         mov    ax, Date    ;First, extract Month
  169.         mov    cl, 4
  170.         shr    ah, cl
  171.         mov    dh, ah        ;LDTOAM needs month in DH.
  172.  
  173.         mov    ax, Date    ;Next get the day.
  174.         shl    ax, 1
  175.         and    ah, 11111b
  176.         mov    dl, ah        ;Day needs to be in DL.
  177.  
  178.         mov    cx, Date    ;Now process the year.
  179.         and    cx, 7fh        ;Strip all but year bits.
  180.  
  181.         print
  182.         byte    "Date: ",0
  183.         LDTOAM            ;Convert to a string
  184.         puts
  185.         free
  186.         putcr
  187.  
  188.  
  189.  
  190.  
  191. Quit:        ExitPgm            ;DOS macro to quit program.
  192. Main        endp
  193.  
  194. cseg        ends
  195.  
  196. sseg        segment    para stack 'stack'
  197. stk        byte    1024 dup ("stack   ")
  198. sseg        ends
  199.  
  200. zzzzzzseg    segment    para public 'zzzzzz'
  201. LastBytes    byte    16 dup (?)
  202. zzzzzzseg    ends
  203.         end    Main
  204.